home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Examples / Text / Textension / Include / Textension.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-21  |  14.5 KB  |  508 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Textension.h
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Written by:    Essam Zaky
  7.  
  8.     Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <2>      1/4/94    EZ        clean up
  13.          <1>      1/4/94    EZ        first checked in
  14.  
  15. */
  16.  
  17.  
  18. #ifndef _Textension_
  19. #define _Textension_
  20.  
  21. #ifndef _ToolBoxDump_
  22. #include "ToolBoxDump.h"
  23. #endif
  24.  
  25. #ifndef _StyledText_
  26. #include "StyledText.h"
  27. #endif
  28.  
  29. #ifndef _Display_
  30. #include "Display.h"
  31. #endif
  32.  
  33. #ifndef _Selection_
  34. #include "Selection.h"
  35. #endif
  36.  
  37. #ifndef _RunObject_
  38. #include "RunObject.h"
  39. #endif
  40.  
  41. #ifndef _RunsRanges_
  42. #include "RunsRanges.h"
  43. #endif
  44.  
  45. #ifdef txtnRulers
  46. #ifndef _RulerObject_
  47. #include "RulerObject.h"
  48. #endif
  49.  
  50. #ifndef _RulersRanges_
  51. #include "RulersRanges.h"
  52. #endif
  53. #endif
  54.  
  55. #ifndef _Frames_
  56. #include "Frames.h"
  57. #endif
  58.  
  59. #ifndef _FrameFormatter_
  60. #include "FrameFormatter.h"
  61. #endif
  62.  
  63. #ifndef _Formatter_
  64. #include "Formatter.h"
  65. #endif
  66.  
  67. #ifndef _TextensionIOSuite_
  68. #include "TextensionIOSuite.h"
  69. #endif
  70.  
  71. //***************************************************************************************************
  72.  
  73.  
  74. //"KeyDown" result
  75. typedef ushort TKeyDownFlags;
  76.  
  77. const TKeyDownFlags kNoKeyDownFlags = 0;
  78. const TKeyDownFlags kKeyDownKnown = 1 << 0;
  79. const TKeyDownFlags kTextensionModif = 1 << 1;
  80. const TKeyDownFlags kContinuousObjectsModif = 1 << 2; //continuous run and/or ruler changes (for setup menus)
  81. const TKeyDownFlags kArrowKey = 1 << 3; 
  82. //••flags 1 << 4 through 1 << 10 are reserved
  83. //***************************************************************************************************
  84.  
  85. //flags input to "CharsChanged"
  86. typedef uchar TCharsChangedFlags;
  87.  
  88. const TCharsChangedFlags kCalcRuns = 1;
  89.  
  90. #ifdef txtnRulers
  91. const TCharsChangedFlags kCalcRulers = 2;
  92. #endif
  93.  
  94. const TCharsChangedFlags kCalcLines = 4;
  95.  
  96. #ifdef txtnRulers
  97. const TCharsChangedFlags kCalcAll = kCalcRuns + kCalcRulers + kCalcLines;
  98. #else
  99. const TCharsChangedFlags kCalcAll = kCalcRuns + kCalcLines;
  100. #endif
  101.  
  102. const TCharsChangedFlags kNoCalc = 0;
  103.  
  104. //***************************************************************************************************
  105.  
  106. const short kKeyDownBufferSize = 50;
  107.  
  108. struct TKeyDownParams {
  109.     TOffsetRange range2Replace;
  110.     
  111.     long countSelectedChars;
  112.     
  113.     short countNewChars;
  114.     
  115.     uchar newChars[kKeyDownBufferSize];
  116.     
  117.     TKeyDownFlags flags;
  118. };
  119. //***************************************************************************************************
  120.  
  121. struct TTextensionHandlers {
  122.     TTextensionHandlers();
  123.     
  124.     CTextensionDisplay* displayHandler;
  125.     
  126.     CFrames* framesHandler; //if nil ==> CMonoFrame is created
  127.     
  128.     CSelection* selectionHandler;
  129.     
  130.     CFormatter* formatHandler;
  131. };
  132. //****************************************************
  133.  
  134. #ifdef txtnNever
  135. struct TTextensionContext {
  136.     CArray*    charsHandler;
  137.     
  138.     CRunsRanges*    runsRanges;
  139.     
  140.     #ifdef txtnRulers
  141.     CRulersRanges* rulersRanges;
  142.     #endif
  143.     
  144.     CTextensionDisplay* displayHandler;
  145.     
  146.     CSelection* selectionHandler;
  147.             
  148.     CFormatter* formatterHandler;
  149.     CFrameFormatter* frameFormatterHandler;
  150. };
  151. //****************************************************
  152. #endif
  153.  
  154. struct TTextDescriptor {
  155.     TTextDescriptor() {
  156.         fTextStream = nil;
  157.         fTextPtr = nil;
  158.         fTextSize = 0;
  159.     }
  160.     
  161.     TTextDescriptor(CStream* stream) {
  162.         fTextStream = stream;
  163.         fTextSize = stream->GetSize();
  164.         fTextPtr = nil;
  165.     }
  166.     
  167.     TTextDescriptor(uchar* textPtr, long textSize) {
  168.         fTextPtr = textPtr;
  169.         fTextSize = textSize;
  170.         fTextStream = nil;
  171.     }
  172.     
  173.     CStream* fTextStream;
  174.     uchar* fTextPtr;
  175.     long fTextSize;
  176. };
  177.  
  178. struct TReplaceParams {
  179.     TReplaceParams(); //initialized to nils
  180.  
  181.     TReplaceParams(const TTextDescriptor& textDesc
  182.                                 , Boolean calcScriptRuns = true, Boolean usePendingRun = true);
  183.  
  184.     TReplaceParams(const TTextDescriptor& textDesc, CObjectsRanges* runs, Boolean plugNewRuns);
  185.  
  186.     TReplaceParams(const TTextDescriptor& textDesc, CRunObject* replaceObj, Boolean takeObjCopy);
  187.     
  188.     TReplaceParams(CTextensionIOSuite* ioSuite);
  189.  
  190.     TReplaceParams(CRunObject* runObj, Boolean takeObjCopy = false);
  191.     //non text run obj (pict, sound, ..)
  192.  
  193. TTextDescriptor fTextDesc;
  194.  
  195. Boolean fCalcScriptRuns;
  196. Boolean fUsePendingRun;
  197.  
  198. CTextensionIOSuite* fIOSuite;
  199.  
  200. CObjectsRanges* fNewRuns;
  201. Boolean fPlugNewRuns;
  202.  
  203. CRunObject* fReplaceObj;
  204. Boolean fTakeObjCopy;
  205.  
  206. Boolean fTrailEdge;
  207. };
  208. //****************************************************
  209.  
  210. class CTextension : public CStyledText {
  211.  
  212.     friend class CSelection;
  213.  
  214.   public:
  215. //--------
  216.     static OSErr TextensionStart(CTextensionScrap* localScrap = nil);
  217.     static void TextensionTerminate();
  218.  
  219.     /*next 2 should be called after TextensionInit.
  220.     the first run obj to RegisterIORun should be the text one
  221.     */
  222.     static void RegisterIORun(CRunObject* theObject);
  223.     
  224.     #ifdef txtnRulers
  225.     static void RegisterIORuler(CRulerObject* theObject);
  226.     #endif
  227.     
  228.     static CRunObject* GetNewRunObject(); //••should be called after calling the first RegisterIORun
  229.  
  230.     static CRunObject* GetDefaultRun() {return fDefaultRun;}
  231.     inline static void SetDefaultRun(CRunObject* defRun) {fDefaultRun = defRun;}
  232.  
  233.     inline static CTextensionScrap* GetLocalScrap()
  234.         {return fLocalScrap;}
  235.  
  236.     inline static void SetImaging(Boolean on_off) {gTextensionImaging = on_off;}
  237.     inline static Boolean IsImaging() {return gTextensionImaging;}
  238.  
  239.     CTextension();
  240.     
  241.     OSErr ITextension(GrafPtr textPort, TTextensionHandlers* handlers, TSize sizeInfo = kLargeSize);
  242.     virtual void Free();
  243.  
  244. //•handlers
  245.     inline CSelection* GetSelectionHandler() {return fSelection;}
  246.     
  247.     #ifdef txtnRulers
  248.     inline CRulersRanges* GetRulersRanges() {return fRulersRanges;}
  249.     #endif
  250.     
  251.     inline CFrames* GetFramesHandler() {return fDisplay->GetFramesHandler();}
  252.     inline CFormatter* GetFormatter() {return fFormatter;}
  253.     inline CFrameFormatter* GetFrameFormatter() {return fFrameFormatter;}
  254.     
  255. //•Frames        
  256.     virtual short DisplayChanged(const CDisplayChanges* displayChanges);
  257.     /*
  258.     •Focus should be made before calling this routine (it may needs to draw).
  259.     
  260.     call it after calling any "Frames" member which returns a "CDisplayChanges".
  261.     After calling "DisplayChanged", caller should test the returned result for the following values:
  262.     -kRedrawAll ==> redraw the whole content of the fFrames.
  263.     -kCheckSize ==> adjust the frames's size.
  264.     -kCheckUpdate ==> some frames has been invalidated, process the update event for fast response.
  265.     */
  266.  
  267. //•Drawing
  268.     inline void GetViewRect(Rect* theRect) const {fDisplay->GetViewRect(theRect);}
  269.     inline void SetViewRect(const Rect* theRect) {fDisplay->SetViewRect(theRect);}
  270.  
  271.     inline void HideDrawPen() {fDisplay->HideDrawPen();}
  272.     inline void ShowDrawPen() {fDisplay->ShowDrawPen();}
  273.     inline void HideSelectionPen() {fSelection->DisableDrawing();}
  274.     inline void ShowSelectionPen() {fSelection->EnableDrawing();}
  275.     
  276.     virtual void Draw(const Rect *updateRect, TDrawFlags drawFlags = kEraseBeforeDraw);
  277.         
  278.         
  279. //•Editing
  280.     virtual void CalcKeyDownParams(const EventRecord* event, Boolean bufferKeys, TKeyDownParams* params);
  281.     TKeyDownFlags KeyDown(const EventRecord* event, TKeyDownParams* params = nil);
  282.     
  283.     OSErr Cut();
  284.     OSErr Copy();
  285.     
  286.     inline static Boolean CanPaste() {return fLocalScrap->CanPaste();}
  287.     
  288.     OSErr Paste();
  289.     void Clear();
  290.  
  291.     inline short GetLastEditAction() {return fDisplay->GetLastEditAction();}
  292.     
  293.     
  294. //•Formatting
  295.     inline void DisableFormatting()  {fFormatter->DisableFormatting();}
  296.     inline void EnableFormatting() {fFormatter->EnableFormatting();}
  297.     virtual OSErr Format(Boolean temporary = false, long firstChar =-1, long count =-1);
  298.     /*does not redraw (unless firstChar and count > 0)
  299.         if "temporary" is true ==> assumed that Format will be called again to restore the old lines ends,
  300.         this is useful for non wysiwyg printing.
  301.         "temporary" can be true only if format all (firstChar < 0)
  302.     */
  303.     
  304. //•Scrolling
  305.     void SetAutoScroll(Boolean flag) {fSelection->SetAutoScroll(flag);} //false by default
  306.     inline void Scroll(const LongPoint* scrollVals) {fDisplay->Scroll(scrollVals);}
  307.     inline void GetScrolledValues(LongPoint* scrolledVals) {fDisplay->GetScrolledValues(scrolledVals);}
  308.     
  309. //•Selection
  310.     virtual void Activate(Boolean activ, Boolean turnSelOn =true);
  311.         
  312.     inline void GetSelectionRange(TOffsetRange* selRange) const {fSelection->GetSelectionRange(selRange);}
  313.     
  314.     virtual void SetSelectionRange(const TOffsetRange& selRange
  315.                                                             , Boolean forceOn = true, Boolean updateKeyScript=true);
  316.     void Idle();
  317.     inline unsigned long GetIdleTime() {return fSelection->GetIdleTime();}
  318.     //Idle should be called after the returned value (in Ticks)
  319.     
  320.     virtual Boolean SetCursor(Point mousePt);
  321.     //see comment in Selection.h
  322.     
  323.     virtual Boolean Click(const EventRecord* event, TClickCommandInfo* clickCommandInfo
  324.                                             , ClickLoopProcPtr clickProc = nil, void* clickProcData = nil);
  325.     
  326.     inline Boolean IsSelectionVisible(const Rect* visRect, LongPoint* scrollVals
  327.                                                                     , short visOption =kCenterSelection, Boolean visSelEnd = true)
  328.         {return fDisplay->IsSelectionVisible(visRect, scrollVals, visOption, visSelEnd);}
  329.     
  330. //•Points
  331.     inline CRunObject* Point2Char(Point thePt, TOffsetRange* charRange, Boolean* outside)
  332.         {return fDisplay->Point2Char(thePt, charRange, outside);}
  333.     //•returns a val < 0 in charRange->rangeStart if the point is not in any frame
  334.     
  335.     CRunObject* Point2Word(Point thePt, TOffsetRange* wordRange, Boolean* outside);
  336.     /* -•returns a val < 0 in wordRange->rangeStart if the point is not in any frame
  337.         -thePt is in local coord
  338.         -the returned result has the same meaning as in Point2Char
  339.         -wordRange->rangeStart is set to wordRange->rangeEnd if no words at that point
  340.     */
  341.     
  342.     inline long Point2Line(Point* thePt, Boolean* outside, Boolean* vOverflow, TOffsetRange* lineRange = nil) const
  343.         {return fDisplay->Point2Line(thePt, outside, vOverflow, lineRange);}
  344.     
  345. //•Chars
  346.     inline void Char2Point(TOffset theChar, long* top, long* trailEdge=nil, short* hite =nil)
  347.         {fDisplay->Char2Point(theChar, top, trailEdge, hite);}
  348.     // top and trailEdge are in abs coord
  349.     //hite in output (if not nil) is set to the line hite of the line containing the char
  350.  
  351.     inline Point Char2Point(TOffset theChar, short* hite =nil)
  352.         {return fDisplay->Char2Point(theChar, hite);}
  353.     //returns the point in draw coord
  354.     
  355.     inline long Char2Line(TOffset charOffset) const
  356.         {return fFormatter->Offset2Line(charOffset);}
  357.  
  358.     #ifdef txtnMultiFrames
  359.     inline long Char2Frame(TOffset charOffset) const
  360.         {return fFrameFormatter->Char2Frame(charOffset);}
  361.     #endif
  362.     
  363.     void Char2Paragraph(long offset, TOffsetRange* paragRange);
  364.     
  365. //•IO
  366.     OSErr GetRangeIOSuite(TOffsetRange* theRange, CTextensionIOSuite* rangeIOSuite, TIOFlags flags = kIOAll);
  367.     //the returned "theRange" should be used for "ReplaceRange". It may be different if the IOSuite contains only rulers data
  368.     
  369.     virtual OSErr ReplaceRange(long rangeStart, long rangeEnd, TReplaceParams* replaceParams, Boolean invisible = false);
  370.  
  371. //•Runs
  372.     virtual OSErr UpdateRangeRuns(const TOffsetRange* theRange, const TAttrObjModifier* modifier
  373.                                                             , Boolean invisible=false);
  374.     //"invisible" true means no formatting or update
  375.     
  376.     CRunObject* GetContinuousRun();
  377.  
  378. #ifdef txtnRulers
  379. //•Rulers
  380.     inline CRulerObject* GetNewRulerObject() const {return fRulersRanges->GetNewRuler();}
  381.     
  382.     inline const CRulerObject* Offset2Ruler(TOffset charOffset) const
  383.         {return (CRulerObject*)(fRulersRanges->Offset2Object(charOffset));}
  384.     //Offset2Ruler returns always a valid ruler (ruler with all attributes set)
  385.     
  386.     OSErr UpdateRangeRulers(const TOffsetRange* theRange, const TAttrObjModifier* modifier, Boolean invisible=false);
  387.     //"invisible" true means no formatting or update
  388. #endif
  389.     
  390. #ifdef txtnAdvRulers
  391.     void CopyRuler();
  392.     void PasteRuler();
  393.     inline Boolean CanPasteRuler() {return fClipboardRuler != nil;}
  394. #endif
  395.  
  396. //•Apply saved data
  397.     virtual void FreeData();
  398.     
  399. #ifdef txtnNever
  400.     //made to be called when read an already saved textension data, call FreeData, change textension data, then call CharsChanged
  401.     OSErr CharsChanged(TCharsChangedFlags flags=kCalcAll);
  402.  
  403. //•context
  404.     void GetContext(TTextensionContext* context) const;
  405.     void SetContext(const TTextensionContext* context);
  406.     virtual long GetActivContextId() const;
  407.     Boolean ActivatContext(long contextId, Boolean turnSelOn =true);
  408.     virtual void ChangeContext(long contextId, Boolean turnSelOn =true);
  409. #endif
  410.  
  411.  
  412. //•Misc.
  413.     virtual void    SetDirection(char newDirection);
  414.     inline char GetDirection() const {return fDisplay->GetDirection();}
  415.     
  416.     #ifdef txtnScal
  417.     virtual void SetScale(Point numer, Point denom);
  418.     inline void GetScale(Point* numer, Point* denom) {fDisplay->GetScale(numer, denom);}
  419.     #endif
  420.     
  421.     inline void SetCRFlag(Boolean newFlag) {fFormatter->SetCRFlag(newFlag);}
  422.     // does NOT have a draw or format effect, caller will normally call format and redraw after this
  423.     inline Boolean GetCRFlag() const {return fFormatter->GetCRFlag();}
  424.     
  425.     inline long    CountLines() const {return fFormatter->CountLines();}
  426.  
  427.     #ifdef txtnScal
  428.     inline long    GetLinesHite(long firstLine, long lastLine, Boolean scaled=true) const
  429.         {return fFrameFormatter->GetLinesHite(firstLine, lastLine, scaled);}
  430.         
  431.     inline long GetTotalLinesHite(Boolean scaled = true) const
  432.         {return fFrameFormatter->GetTotalLinesHite(scaled);}
  433.     #else
  434.     inline long    GetLinesHite(long firstLine, long lastLine) const
  435.         {return fFrameFormatter->GetLinesHite(firstLine, lastLine);}
  436.         
  437.     inline long GetTotalLinesHite() const
  438.         {return fFrameFormatter->GetTotalLinesHite();}
  439.     #endif
  440.     
  441.     protected:
  442. //----------
  443.     CTextensionDisplay* fDisplay;
  444.     
  445.     CSelection*        fSelection;
  446.             
  447.     CFormatter*        fFormatter;
  448.     CFrameFormatter* fFrameFormatter;
  449.     
  450.     #ifdef txtnRulers
  451.     CRulersRanges*    fRulersRanges;
  452.     #endif
  453.     
  454.     virtual CRunObject* UpdatePendingRun();
  455.  
  456.     #ifdef txtnNever
  457.     virtual OSErr NewRunsInstalled(long offset, long len, Boolean invisible = false);
  458.     //"invisible" true means no formatting or update
  459.     #endif
  460.     
  461.     virtual void BeginEdit(TEditInfo* editInfo);
  462.     virtual void EndEdit(const TEditInfo* editInfo, long firstFormatLine, long lastFormatLine
  463.                                                 , TOffset* selOffset=nil, Boolean updateKeyScript=true);
  464.     
  465.   private:
  466. //--------
  467. CRunObject*    fPendingRun;
  468.  
  469. uchar fBufferedChar;
  470.  
  471. static CRunObject* fDefaultRun;
  472.  
  473. #ifdef txtnAdvRulers
  474. static CRulerObject* fClipboardRuler;
  475. #endif
  476.  
  477. static CTextensionScrap* fLocalScrap;
  478.  
  479. //•Editing
  480.     long ClearKeyDown(uchar key, TOffsetRange* range2Clear);
  481.     
  482.     OSErr UpdateFormatter(long updateFlags, const TOffsetRange* range
  483.                                             , long* firstFormatLine, long* lastFormatLine);
  484.     
  485. //•Runs        
  486.     CRunObject* IsRangeGraphicsObj(const TOffsetRange* charRange =nil);
  487.  
  488. //•for CSelection
  489.     void Point2Paragraph(Point thePt, TOffsetRange* paragRange, Boolean* outside);
  490.     //•returns a val < 0 in paragRange->rangeStart if the point is not in any frame
  491.     
  492.     /*
  493.     void Point2Ruler(Point thePt, TOffsetRange* rulerRange);
  494.     //•returns a val < 0 in rulerRange->rangeStart if the point is not in any frame
  495.     */
  496.  
  497. //•Misc
  498.     void Compact();
  499.     
  500.     void UpdateKeyboardScript();
  501.  
  502. //•input/output
  503. };
  504. //***************************************************************************************************
  505.  
  506.  
  507. #endif
  508.